package com.chengshi.shop.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.chengshi.shop.dao.admin.AdminLogMapper;
import com.chengshi.shop.model.admin.AdminLog;
import com.chengshi.shop.model.admin.AdminUser;
import com.chengshi.shop.util.IpUtil;
import com.chengshi.shop.util.SessionUtils;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xuxinlong
 * @version 2017-11-21
 */
@Aspect
@Component
public class WebLogAspect {
    private ThreadLocal<Integer> logId = new ThreadLocal<>();
    @Autowired
    private AdminLogMapper adminLogMapper;

    /**
     * 定义一个切入点
     */
    @Pointcut("execution(* com.chengshi.shop.controller.admin..*Controller.save*(..)) ||" +
            " execution(* com.chengshi.shop.controller.admin..*Controller.update*(..)) || " +
            "execution(* com.chengshi.shop.controller.admin..*Controller.delete*(..)) ||" +
            "execution(* com.chengshi.shop.controller.admin..*Controller.close*(..)) ||" +
            "execution(* com.chengshi.shop.controller.admin..*Controller.up*(..)) ||" +
            "execution(* com.chengshi.shop.controller.admin..*Controller.down*(..))")
    public void webLog() {
    }

    @Before("webLog()")
    public void doBefore() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        AdminLog adminLog = new AdminLog();
        AdminUser adminUser = SessionUtils.getCurrentUser();
        if (adminUser != null) {
            adminLog.setUserId(adminUser.getUserId());
        }
        adminLog.setLogUrl(request.getServletPath());
        adminLog.setIpAddress(IpUtil.getIpAddress(request));
        adminLog.setCreateTime(new Date());
        // 获取所有请求参数
        Enumeration<String> enu = request.getParameterNames();
        //参数名和值存放到map中
        Map<String, Object> parammaps = new HashMap<>();
        while (enu.hasMoreElements()) {
            String paraName = enu.nextElement();
            System.out.println(paraName + ": " + request.getParameter(paraName));
            parammaps.put(paraName, request.getParameter(paraName));
        }
        adminLog.setLogInfo(JSON.toJSONString(parammaps, SerializerFeature.PrettyFormat));
        adminLogMapper.insertSelective(adminLog);
        logId.set(adminLog.getLogId());
    }

    @AfterReturning(pointcut = "webLog()", returning = "rvt")
    public void doAfterReturning(Object rvt) {
        // 处理完请求，返回内容
        AdminLog adminLog = new AdminLog();
        adminLog.setLogId(logId.get());
        adminLog.setCreateTime(new Date());
        adminLog.setLogDesc(JSON.toJSONString(rvt, SerializerFeature.PrettyFormat));
        adminLogMapper.updateByPrimaryKeySelective(adminLog);
        logId.remove();
    }

}